package com.mymarkdown.app.fx.event.hander.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.mymarkdown.app.fx.annotation.KeyWord;
import com.mymarkdown.app.fx.enums.FxEventEnum;
import com.mymarkdown.app.fx.event.hander.KeyWordHandler;
import com.mymarkdown.app.fx.event.model.FxEvent;
import javafx.scene.web.WebEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zenglun
 * @date 2021/12/7 9:10 上午
 * @description: 中央处理分发器,用于将键盘事件分发给所有实现了KeyWordHandler接口的bean对象进行处理
 */
@Slf4j
@Primary
@Component("selectKeyWordHandlerImpl")
public class SelectKeyWordHandlerImpl implements KeyWordHandler {

    @Autowired
    private List<KeyWordHandler> keyWordHandlers;

    private Map<FxEventEnum, KeyWordHandler> keyWordHandlerMap = new LinkedHashMap<>();

    @PostConstruct
    public void init() {
    }


    @Override
    public void handler(WebEngine engine, FxEvent event) {
        if (CollectionUtil.isEmpty(keyWordHandlers)) {
            log.error("[SelectKeyWordHandlerImpl]---[handler],没有找到相关处理键盘事件的类,事件类型为:{}", event);
            return;
        }
        //初始化
        initKeyWordHandlerMap();
        FxEventEnum eventType = event.getEventType();
        if (Objects.isNull(eventType)) {
            log.warn("[SelectKeyWordHandlerImpl]---[handler],传递处理的事件类型为空, 事件为:{}", event);
            return;
        }
        KeyWordHandler keyWordHandler = keyWordHandlerMap.get(eventType);
        if (Objects.isNull(keyWordHandler)) {
            log.error("[SelectKeyWordHandlerImpl]---[handler],没有找到相关处理bean,事件为:{}", event);
            return;
        }
        try {
            if (keyWordHandler.afterHandler(engine, event)) {
                keyWordHandler.handler(engine, event);
            }
        } catch (Exception e) {
            //TODO 处理事件发生异常则通知用户发生异常了
            e.printStackTrace();
            log.error("[SelectKeyWordHandlerImpl]---[handler],处理事件发生异常,事件类型为:{},异常信息为:{}", event, e);
        } finally {
            keyWordHandler.beforeHandler(engine, event);
        }
    }

    private void initKeyWordHandlerMap() {
        if (MapUtil.isNotEmpty(keyWordHandlerMap)) {
            return;
        }
        keyWordHandlers.forEach(e -> {
            Class<? extends KeyWordHandler> aClass = e.getClass();
            KeyWord annotation = aClass.getAnnotation(KeyWord.class);
            if (Objects.isNull(annotation)) {
                log.warn("[SelectKeyWordHandlerImpl]---[initKeyWordHandlerMap],找到一个未标识处理键盘事件类型的bean,class:{}", aClass);
                return;
            }
            //如果不等于null,
            keyWordHandlerMap.put(annotation.value(), e);
        });

    }

    @Override
    public boolean afterHandler(WebEngine engine, FxEvent event) {
        return true;
    }

    @Override
    public void beforeHandler(WebEngine engine, FxEvent event) {

    }

}
